#include<bits/stdc++.h>
using namespace std;
#define ans first
#define loc second
const int MAXN=210;
int n,m,k,_x[]={0,-1,1,0,0},_y[]={0,0,0,-1,1},f[MAXN][MAXN],ans;
bool check[MAXN][MAXN];
deque<pair<int,int> >q;
void solve(int x,int y,int dir,int length)
{
	q.clear();
	for(int i=0;;++i,x+=_x[dir],y+=_y[dir])
	{
		if(x>n||!x||y>m||!y)break;
		if(check[x][y])
		{
			q.clear();
			continue;
		}
		while(!q.empty()&&q.back().ans+(i-q.back().loc)<=f[x][y])q.pop_back();
		q.push_back(make_pair(f[x][y],i));
		while(!q.empty()&&i-q.front().loc>length)q.pop_front();
		f[x][y]=q.front().ans+i-q.front().loc;
		ans=max(ans,f[x][y]);
	}
	return;
}
int main()
{
	int x,y;
	scanf("%d%d%d%d%d",&n,&m,&x,&y,&k);
	for(int i=1;i<=n;++i)
	{
		char temp[MAXN];
		scanf("%s",temp+1);
		for(int j=1;j<=m;++j)if(temp[j]=='x')check[i][j]=true;
	}
	memset(f,0xef,sizeof(f));
	f[x][y]=0;
	while(k--)
	{
		int t1,t2,t3;
		scanf("%d%d%d",&t1,&t2,&t3);
		if(t3==1)for(int i=1;i<=m;++i)solve(n,i,t3,t2-t1+1);
		else if(t3==2)for(int i=1;i<=m;++i)solve(1,i,t3,t2-t1+1);
		else if(t3==3)for(int i=1;i<=n;++i)solve(i,m,t3,t2-t1+1);
		else for(int i=1;i<=n;++i)solve(i,1,t3,t2-t1+1);
	}
	printf("%d\n",ans);
	return 0;
}
